<script setup>
import * as api from "@/api/v-sys-service/exam/question";
import examQuestionFormDialog from "@/views/v-sys-service/exam/question/question-form-dialog.vue"
import Constants from "@/const/index.js";

const props = defineProps({
  rowData: {
    type: Object,
    default: () => {}
  }
})

/**
 * 组件挂载后
 */
onMounted(() => {
  init()
})


/**
 * 列表数据
 */
const tableData = ref([])

const optionTags = ref([
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
])

/**
 * loading状态
 */
const loading = ref({
  tableLoading: false,// 列表loading
})

/**
 * 弹窗显隐
 */
const dialog = ref({
  showFormDialog: false, //表单弹窗
  showImportExcelDialog: false, //excel导入弹窗
})

/*watch(
    props.rowData,
    (val) =>{
      console.log(val)
      // if (val && val.id) title.value = "" + val.name + " 字典数据"
    },
    {
      immediate: true,
    }
)*/

const rowData = ref({})

/**
 * 计算表格属性
 */
const tableInfo = computed(()=>{
  const tableProps = {
    data: tableData.value,
    loading: loading.value.tableLoading,
    rowConfig: { keyField: 'id' },
    checkboxConfig: { highlight: false, reserve: true },
    showSelectCount: false,
    showExportBtn: false,
  }
  return { ...tableProps }
})

/**
 * 分页参数
 */
const pageQuery = reactive({
  page: 1,
  limit: 50,
  total: 0,
  url: '/cloud-sys-service/exam-question/page',
  orders: [
    {column: "createTime", asc: false}
  ],
  filters: [],
  condition: {
    key: "",
    groupId: props.rowData.id
  },
})

/**
 * 导入选项
 */
const importOptions = ref({
  uploadUrl: '/cloud-sys-service/exam-question/importExcel',
  params: {
    groupId: props.rowData.id
  },//额外参数
  downloadUrl: '/cloud-sys-service/exam-question/downloadTemplate',
  importParamName: 'multipartFile',
  importBtnName: '上传',
})

/**
 * 组件初始化调用函数
 */
const init = () => {
  if (props.rowData.id) init_PageQuery()
}

/**
 * 分页查询
 */
const init_PageQuery = async () => {
  loading.value.tableLoading = true
  const { code, data } = await api.page(pageQuery)
  if (code === 200) {
    loading.value.tableLoading = false
    tableData.value = data.records
    pageQuery.total = data.total
  }
}

/**
 * form表单
 */
const showFormDialog = ($event,row) => {
  if (row && row.id) rowData.value = row
  else rowData.value = {}
  dialog.value.showFormDialog = true
  rowData.value.groupId = props.rowData.id
}

/**
 * 删除
 */
const showRemoveDialog = ($event,row) => {
  ElMessageBox.confirm('', '确定删除吗',
      {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '拼命加载中...'
            api.removeById(row.id).then(({code})=>{
              instance.confirmButtonLoading = false
              if (code === 200){
                ElMessage({message: '删除成功', type: 'success',})
                init_PageQuery()
              }
            })
            done()
          } else {
            done()
          }
        }
      }
  )
}

/**
 * 组件对外提供可访问数据
 */
defineExpose({
  showFormDialog,
  showRemoveDialog,
})

</script>

<template>
  <div class="exam-question-box flex flex-col">
    <div class="btn">
      <div class="flex align-center">
        <label class="label">{{'\u2003'}}关键字{{'\u2003'}}</label>
        <el-input
            v-model="pageQuery.condition.key"
            style="width: 180px;" placeholder="请输入关键字"
            @keyup.enter="init_PageQuery" @clear="init_PageQuery" clearable
        />
        <div class="el-button" style="display: none"></div>
        <el-button type="primary" @click="init_PageQuery">
          <template #icon><i-ep-search /></template>搜索
        </el-button>
        <el-button type="primary" @click="showFormDialog">
          <template #icon><i-ep-plus /></template>添加试题
        </el-button>
        <el-button type="primary" @click="dialog.showImportExcelDialog = true">
          <template #icon><i-ep-plus /></template>导入试题
        </el-button>
      </div>
    </div>
    <v-table
        v-model="pageQuery"
        :table-info="tableInfo"
        :column-info="api.columnInfo"
        @query="init_PageQuery"
    >
      <template #questionType="{row}">
        <el-tag v-if="row.questionType === Constants.RADIO" type="warning">单选题</el-tag>
        <el-tag v-if="row.questionType === Constants.CHECK" type="success">多选题</el-tag>
        <el-tag v-if="row.questionType === Constants.TRUE_FALSE" type="primary">判断题</el-tag>
      </template>
      <template #operate="{row}">
        <el-button type="primary" :text="true" size="small" @click="showFormDialog($event,row)">
          <template #icon><i-ep-edit /></template>编辑
        </el-button>
        <el-button type="primary" :text="true" size="small" @click="showRemoveDialog($event,row)">
          <template #icon><i-ep-delete /></template>删除
        </el-button>
      </template>
      <vxe-column
          align="center"
          width="100"
          type="expand"
          title="题目选项"
          fixed="left"
      >
        <template #content="{ row }">
          <vxe-table class="expand-wrapper" border :data="row.answerOptions">
            <vxe-column
                field="optionTag"
                min-width="60"
                title="选项"
            >
              <template #default="{rowIndex}">{{optionTags[rowIndex]}}</template>
            </vxe-column>
            <vxe-column
                field="answer"
                min-width="400"
                title="选项内容"
            ></vxe-column>
            <vxe-column
                field="isRightAnswer"
                min-width="60"
                title="是否正确答案"
            >
              <template #default="{row}">{{row.isRightAnswer ? '是': '否'}}</template>
            </vxe-column>
          </vxe-table>
        </template>
      </vxe-column>
    </v-table>
    <!--  考试题库表单    -->
    <examQuestionFormDialog v-if="dialog.showFormDialog" @query="init_PageQuery" v-model="dialog.showFormDialog" :row-data="rowData" />

    <!--  导入试题  -->
    <v-import-excel v-if="dialog.showImportExcelDialog" @query="init_PageQuery" v-model="dialog.showImportExcelDialog" :import-options="importOptions"></v-import-excel>

  </div>
</template>

<style scoped lang="scss">
  .exam-question-box {
    padding: 10px;
    .btn {
      text-align: right;
      position: absolute;
      top: -35px;
      right: 0;
      padding-right: 20px;
    }
    .container {
      :deep(.el-tag) {
        cursor: pointer;
      }
      :deep(.el-button) {
        padding: 0;
      }
    }
    .expand-wrapper {
      padding: 20px 40px;
    }
  }
</style>
